package basewsclient

import (
	"gitee.com/851537/gbase"
	"github.com/gorilla/websocket"
)

type IWsClient interface {

	//由base实现
	Connect(child IWsClient)
	CheckConn() bool
	Send(child IWsClient, msg []byte) error

	//获取消息处理观察者集线器,由实体类可选实现
	GetMsgObserverHub() *gbase.ObserverHub

	//注册消息观察者
	RegMsgObserver(observer gbase.IObserver)

	//设置名称
	SetName(name string)

	//设置url
	SetUrl(url string)

	//设置debug
	SetDebug(status bool)

	//设置pingInterval
	SetPingInterval(interval int64)

	//设置ping内容
	SetPingContent(pingContent string)

	//设置ping类型
	SetPingType(pingType int)

	//设置pingHandler
	SetPingHandler(func(appData string) error)
	SetPongHandler(func(appData string) error)

	//设置onMessage
	SetOnMessage(func(child IWsClient, msg []byte, msgType ...int))

	//设置onConnected
	SetOnConnected(func(child IWsClient))

	//设置onDisconnect
	SetOnDisconnect(func(child IWsClient, err error))

	GetConn() *websocket.Conn
	GetName() string
	GetUrl() string
}

func WithName(name string) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetName(name)
	}
}

func WithUrl(url string) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetUrl(url)
	}
}

func WithDebug(status bool) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetDebug(status)
	}
}

func WithPingInterval(interval int64) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetPingInterval(interval)
	}
}

func WithPingHandler(f func(appData string) error) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetPingHandler(f)
	}
}

func WithPingContent(pingContent string) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetPingContent(pingContent)
	}
}

func WithPingType(pingType int) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetPingType(pingType)
	}
}

func WithPongHandler(f func(appData string) error) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetPongHandler(f)
	}
}

func WithOnMessage(fn func(child IWsClient, msg []byte, msgType ...int)) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetOnMessage(fn)
	}
}

func WithOnConnected(fn func(child IWsClient)) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetOnConnected(fn)
	}
}

func WithOnDisconnect(fn func(child IWsClient, err error)) gbase.Option[IWsClient] {
	return func(obj IWsClient) {
		obj.SetOnDisconnect(fn)
	}
}
